package com.qiangesoft.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiangesoft.mybatisplus.entity.SysUser;
import com.qiangesoft.mybatisplus.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @since 2024-04-11
 */
@RestController
@RequestMapping("/sys-user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @GetMapping("/page")
    public Page<SysUser> page() {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SysUser::getCreateTime);
        return sysUserService.page(new Page<>(1, 10), queryWrapper);
    }

    @GetMapping("/list")
    public List<SysUser> list() {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SysUser::getCreateTime);
        return sysUserService.list(queryWrapper);
    }

    @GetMapping("/save")
    public Boolean save() {
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(1L);
        sysUser.setUserName("zhangsan");
        sysUser.setPassword("123456");
        sysUser.setNickName("张三");
        sysUser.setPhoneNumber("13111111111");
        sysUser.setSex("0");
        sysUser.setStatus("0");
        sysUser.setVersion(1);
        sysUser.setDeleted(false);

        return sysUserService.save(sysUser);
    }

    @GetMapping("/saveBatch")
    public Boolean saveBatch() {
        List<SysUser> sysUserList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            SysUser sysUser = new SysUser();
            sysUser.setDeptId(1L);
            sysUser.setUserName("zhangsan" + i);
            sysUser.setPassword("123456");
            sysUser.setNickName("张三" + i);
            sysUser.setPhoneNumber("1311111111" + i);
            sysUser.setSex("0");
            sysUser.setStatus("0");
            sysUser.setVersion(1);
            sysUser.setDeleted(false);
            sysUserList.add(sysUser);
        }

        return sysUserService.saveBatch(sysUserList);
    }

    @GetMapping("/remove")
    public Boolean remove() {
        LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysUser::getId, 1);
        return sysUserService.remove(updateWrapper);
    }

    @GetMapping("/removeById")
    public Boolean removeById() {
        return sysUserService.removeById(1);
    }

    @GetMapping("/removeBatchByIds")
    public Boolean removeBatchByIds() {
        return sysUserService.removeBatchByIds(Collections.singletonList(1));
    }

    @GetMapping("/update")
    public Boolean update() {
        LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysUser::getId, 1)
                .eq(SysUser::getPhoneNumber, "13222222222");

        return sysUserService.update(updateWrapper);
    }

    @GetMapping("/updateById")
    public Boolean updateById() {
        SysUser sysUser = new SysUser();
        sysUser.setId(1L);
        sysUser.setPhoneNumber("13222222222");

        return sysUserService.updateById(sysUser);
    }

    @GetMapping("/updateBatchById")
    public Boolean updateBatchById() {
        List<SysUser> sysUserList = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            SysUser sysUser = new SysUser();
            sysUser.setId((long) i);
            sysUser.setPhoneNumber("1511111111" + i);
            sysUserList.add(sysUser);
        }

        return sysUserService.updateBatchById(sysUserList);
    }

    /**
     * 常用的sql方法
     */
    private void demoSql() {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                // 最前的sql
                .first("")
                // 查询字段
                .select(SysUser::getId, SysUser::getUserName, SysUser::getPhoneNumber)
                // 等于
                .eq(SysUser::getId, 1)
                // 不等于
                .ne(SysUser::getId, 0)
                // 模糊查询
                .like(SysUser::getNickName, "张")
                // 左模糊查询 '%*'
                .likeLeft(SysUser::getNickName, "张")
                // 右模糊查询 '*%'
                .likeRight(SysUser::getNickName, "张")
                // 大于等于
                .ge(SysUser::getCreateTime, LocalDateTime.now())
                // 小于等于
                .le(SysUser::getUpdateTime, LocalDateTime.now())
                // 大于
                .gt(SysUser::getCreateTime, LocalDateTime.now())
                // 小于
                .lt(SysUser::getCreateTime, LocalDateTime.now())
                // 介于之间
                .between(SysUser::getCreateTime, LocalDateTime.now().minusDays(7), LocalDateTime.now())
                // 不介于之间
                .notBetween(SysUser::getCreateTime, LocalDateTime.now().minusDays(7), LocalDateTime.now())
                // in
                .in(SysUser::getId, 1)
                // not in
                .notIn(SysUser::getId, 3)
                // 为空
                .isNull(SysUser::getDeptId)
                // 不为空
                .isNotNull(SysUser::getUserName)
                // 和
                .and(i -> i.eq(SysUser::getUserName, "李白")
                        // 或
                        .or()
                        .ne(SysUser::getDeleted, true))

                // 倒序
                .orderByDesc(SysUser::getCreateTime)
                // 正序
                .orderByAsc(SysUser::getId)
                // 存在
                .exists("select id from table where age = 1")
                // 函数
                .func(null)
                // 分组
                .groupBy(SysUser::getDeptId)
                // 过滤
                .having("sum(age) > 100")
                // 拼接sql
                .apply("")
                // 最后的sql
                .last("limit 1")
        ;

        System.out.println(queryWrapper.getTargetSql());
    }

}

